<?php

class Storefront_Validate_UniqueEmail extends Zend_Validate_Abstract
{
    
    const EMAIL_EXISTS = 'emailExists';
    
    protected $_messageTemplate = array(
        self::EMAIL_EXISTS => 'Email "%value%" already exists in our system',
    );
    
    public function __construct(Storefront_Model_User $model) 
    {
        
        $this->_model = $model;
    
    }
    
    public function isValid($value,  $context = NULL) 
    {
        
        $this->_setValue($value);
        
        // @var $currentUser Storefront_Resource_User_Item | null 
        $currentUser = isset ($context['userId']) ?
            $this->_model->getUserById($context['userId']) : null;
        
        $user = $this->_model->getUserByEmail($value, $currentUser);
        
        if(null === $user) {
            return TRUE;
        }
        
        $this->_error(self::EMAIL_EXISTS);
        return FALSE;
        
    }
    
}